/**
 * 微信-倾向性
 */
(function ($, window, document, undefined) {
    var comChart;
    var WechatTendency = function (el, opts) {
        this.el = el;
        this.defaults = {
            theme: 'macarons',
            chartOption:{
                title : {
                    text: '文章内容倾向性',
                    subtext: '',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient : 'vertical',
                    x : 'left',
                    data: ['正向', '负向', '中立']
                },
                calculable : true,
                series : [
                    {
                        name:'倾向性',
                        type:'pie',
                        radius : '55%',
                        center: ['50%', '60%'],
                        data:[
                            {value:0, name:'正向'},
                            {value:0, name:'负向'},
                            {value:0, name:'中立'}
                        ]
                    }
                ]
            }
        };
        this.options = $.extend({}, this.defaults, opts)
    };

    WechatTendency.prototype = {
        init: function () {
            var me = this,
                el = me.el;

            if (el.attr('data-initialized') == true) {
                return me;
            }

            me.initChart();
            me.bindEvents();

            el.attr('data-initialized', 'true');
            return me;
        },

        /**
         * [bindEvents description]
         * @return {[type]} [description]
         */
        bindEvents: function () {
            var me = this,
                el = me.el;
        },

        formatData: function(originData){
            var me = this,
                el = me.el,
            chartOption = me.options.chartOption;


            //初始化series
            chartOption.series[0].data[0].value = originData['positive'];
            chartOption.series[0].data[1].value = originData['negative'];
            chartOption.series[0].data[2].value = originData['neutral'];

            return chartOption;
        },

        initChart: function () {
            var me = this,
                el = me.el,
                option={};


            comChart = echarts.init(el[0], me.options.theme);
            option = me.formatData(me.options.data);
            comChart.setOption(option);
//            $.ajax({
//                url: $.fn.utils.env == 'dev' ? '/keywords/1/wechat_tendency.json' : '',
//                type: 'GET',
//                dataType: 'json',
//                data: {},
//                success: function(ret) {
//                    if (ret && ret.status) {
//                        var data = ret.data[0].wechat_tendency;
//
//                        option = me.formatData(data);
//                        comChart.setOption(option);
//                    } else {
//
//                    }
//                },
//                error: function(ret) {
//
//                }
//            });
        }



    };

    $.fn.WechatTendency = function (opts) {
        var me = this,
            el = me.el,
            comWechatTendency;

        // 如果没有被初始化过
        if (!(el && el.length && el.attr('data-initialized') != 'true')) {
            comWechatTendency = new WechatTendency(this, opts);
        }

        return comWechatTendency.init();
    };


})(jQuery, window, document);
